<?php
/**
 * Created By Project
 * Author: 陈东
 * Copyright：陈东
 * Source Code：陈东
 * Explain：I shall not authorize for any other commercial purposes, law breakers must be prosecuted.
 */
namespace Home\Controller;
use Think\Controller;

class ApforController extends Controller {

	public function apforadmin(){
		$promotion = M("apply_promotion",null);
		$res = $promotion->order('apply_state')->select();
		$this->assign('list', $res);
		$this->display('Apply/apforadmin');
	}

    public function index(){
    	$this->display('Apply/popularize');
    }

	//申请推介
	public function applypop(){
		$apply_unit 	= trim(I('post.apply_unit'));
		$apply_pepole 	= trim(I('post.apply_pepole'));
		$apply_tel 		= trim(I('post.apply_tel'));
		$apply_email 	= trim(I('post.apply_email'));
		$apply_reason 	= trim(I('post.apply_reason'));
		$rules = array(
			array('apply_unit','require','请填写申请单位！'),
			array('apply_pepole','require','请填写申请人！'),
			array('apply_tel','require','请填写联系电话！'),
			array('apply_reason','require','请填写申请理由！'),
		);

		if($this->checkinfo($apply_unit, $apply_pepole, $apply_tel)){
			$this->error('已存在申请信息，请勿重复提交','',1);
		}else{
			$promotion = M("apply_promotion",null);
			if(!$promotion->validate($rules)->create()){
				$this->error($promotion->getError(),'',2);
			}else{
				$promotion->apply_unit 		= $apply_unit;
				$promotion->apply_pepole	= $apply_pepole;
				$promotion->apply_tel 		= $apply_tel;
				$promotion->apply_email 	= $apply_email;
				$promotion->apply_reason 	= $apply_reason;
				$promotion->apply_isagree 	= 1;
				$promotion->apply_time		= date("Y-m-d H:i:s", time());
				$result = $promotion->add();
				if($result){
					$this->send_msg($apply_pepole, $apply_unit, $apply_tel, $apply_email, $apply_reason);
					$this->send_bakmsg($apply_tel);
					$this->success('提交申请成功','/Apfor/index/sussce/ok');
				}else{
					$this->error('提交申请失败','/Apfor/index/sussce/no',1);
				}
			}
		}
	}

	/**
	 * 提交成功发送短信给申请人
	 */
	protected function send_bakmsg($mobile){
		$fun= new \Org\Util\fun();
		$fun->Send_bakremindMsg($mobile);
	}

	/**
	 * 提交成功发送短信给接收人
	 */
	protected function send_msg($proposer, $company, $tel, $email, $apply_reason){
		$fun= new \Org\Util\fun();
		$fun->Send_remindMsg($proposer, $company, $tel, $email, $apply_reason);
	}

	/**
	 * 检测申请信息是否已被填写过
	 */
	protected function checkinfo($apply_unit, $apply_pepole, $apply_tel){
		$promotion = M("apply_promotion",null);
		$res = $promotion->where(array('apply_unit' => $apply_unit, 'apply_pepole' => $apply_pepole, 'apply_tel' => $apply_tel))->select();
		if($res){
			return true;
		}else{
			return false;
		}
	}

}